﻿using IdGen;
using Microsoft.Extensions.Options;

namespace PMIS.WebApi.SnowflakeId
{
    public class SnowflakeIdGenerator
    {
        private readonly IdGenerator _idGenerator;

        public SnowflakeIdGenerator(IOptions<SnowflakeIdOptions> options)
        {
            var generatorId = options.Value.GeneratorId; // 从配置中读取生成器ID，确保在分布式系统中是唯一的
            var epoch = new DateTime(2020, 4, 1, 0, 0, 0, DateTimeKind.Utc);
            var structure = new IdStructure(45, 2, 16);
            var idOptions = new IdGeneratorOptions(structure, new DefaultTimeSource(epoch));
            _idGenerator = new IdGenerator(generatorId, idOptions);
        }

        public string GetId()
        {
            return _idGenerator.CreateId().ToString();
        }
    }
}
